package com.itbaizhan.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY;
import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_RETRIEVE_SIZE_KEY;

@RestController
public class ChatMemoryController {


    @Autowired
    ChatClient chatClient;

    /**
     * @param id      用户id
     * @param message 消息
     * @return
     */
    @GetMapping("/chat")
    public String chat(String id, String message) {
        // 新对话
        ChatResponse chatResponse = chatClient.prompt()
                // 用户输入消息
                .user(message)
                // 配置聊天顾问  添加聊天会话  id
                .advisors(spec -> spec.param(CHAT_MEMORY_CONVERSATION_ID_KEY, id).param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 10))
                // 发起调用 执行聊天请求
                .call()
                // 获取聊天内容
                .chatResponse();

        // 获取聊天内容
        String content = chatResponse.getResult().getOutput().getContent();
        return content;
    }
}
